import Vue from 'vue'
import VueRouter from 'vue-router'
import menuList from './menuList'
import about from './about'
import service from './service'
import personalCenter from './personalCenter'
import myChange from './myChange'
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

let formatRouterList = [];
function formatRouter(list) {
  for (let item of list) {
    if (item.children) {
      formatRouter(item.children);
      item.children = null;
      let obj = Object.assign({}, item);
      obj.children = null;
      formatRouterList.push(item);
    } else {
      formatRouterList.push(item);
    }
  }
}
formatRouter(menuList)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/',
    component: () => import('@/components/pageComponents/Layout.vue'),
    children: [
      ...formatRouterList,//menuList导航上面有的
      ...about,//关于我们
      ...service,//服务声明
      ...personalCenter,//个人中心
      ...myChange,//我的
      {
        path: '/downloadApp',
        name: '下载app',
        component: () => import('../views/downloadApp/downloadApp.vue')
      },
    ]
  },
]

const router = new VueRouter({
  // mode: 'history',
  routes: [
    ...routes,
  ]
})

router.beforeEach((to, from, next) => {
  next();
  // let token = sessionStorage.getItem('token');
  // if (token) {
  //   next()
  // } else {
  //   if (to.path == '/login') { //如果是登录页面路径，就直接next()
  //     next();
  //   } else { //不然就跳转到登录；
  //     next('/login');
  //   }
  // }


})
export default router
